﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GVbind();
            }
        }
        public void GVbind()
        {
            string sql = "select * from StudentInfo";
            GVStudnet.DataSource = SqlHelper.ExcuteDataTable(sql, null);
            GVStudnet.DataKeyNames = new string[] { "stu_id"};
            GVStudnet.DataBind();
        }
        protected void GVStudnet_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GVStudnet.EditIndex = e.NewEditIndex;
            GVbind();
        }

        protected void GVStudnet_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string id = GVStudnet.DataKeys[e.RowIndex].Value.ToString();
            string name = (GVStudnet.Rows[e.RowIndex].Cells[0].Controls[0] as TextBox)?.Text;
            string email = (GVStudnet.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox)?.Text;
            string intro = (GVStudnet.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox)?.Text;
            string pwd = (GVStudnet.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox)?.Text;

            string sql = "update StudentInfo set stu_name = @name,email=@email,intro=@intro," +
                "password = @pwd where stu_id = @id";
            SqlParameter[] p =
            {
                new SqlParameter("@name",name),
                new SqlParameter("@email",email),
                new SqlParameter("@intro",intro),
                new SqlParameter("@pwd",pwd),
                new SqlParameter("@id",id),
            };
            int count = SqlHelper.ExecuteNonQuery(sql,p);
            if (count > 0)
            {
                Response.Write("<script>alert('更新成功')</script>");
            }
            else
            {
                Response.Write("<script>alert('更新失败')</script>");
            }
            GVbind();
        }

        protected void GVStudnet_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GVStudnet.EditIndex = -1;
            GVbind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            string sql = "select * from StudentInfo where stu_name like '%' + @name + '%'";
            SqlParameter[] p =
            {
                new SqlParameter("@name",tbxName.Text)
            };

            GVStudnet.DataSource = SqlHelper.ExcuteDataTable(sql, p);
            GVStudnet.DataKeyNames = new string[] { "stu_id" };
            GVStudnet.DataBind();

        }

        protected void GVStudnet_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GVStudnet.DataKeys[e.RowIndex].Value.ToString();
            string sql = "delete from StudentInfo where stu_id = @id";
            SqlParameter[] p =
            {
                new SqlParameter("@id",id)
            };
            int count = SqlHelper.ExecuteNonQuery(sql,p);
            if (count > 0)
            {
                Response.Write("<script>alert('删除成功')</script>");
            }
            else
            {
                Response.Write("<script>alert('删除失败')</script>");
            }
            GVbind();
        }
    }
}